<!DOCTYPE HTML>
<html>
<head>
<title><%=title%></title>
<meta name='referrer' content='always'/>
<link rel='shortcut icon' href='<%=icon%>'/>
<meta http-equiv='x-ua-compatible' content='ie=edge,chrome=1'/>
<meta http-equiv='content-type' content='text/html; charset=utf-8'/>
<meta name='viewport' content='width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no'/>

<link rel='stylesheet' type='text/css' href='/app/workspace/css/base.css'/>
<link rel='stylesheet' type='text/css' href='/app/workspace/css/menupad.css'/>
<link rel='stylesheet' type='text/css' href='/app/workspace/css/notepad.css'/>
<link rel='stylesheet' type='text/css' href='/res/lib/codemirror/lib/codemirror.css'/>
<link rel='stylesheet' type='text/css' href='/res/lib/codemirror/addon/fold/foldgutter.css'/>

<script>
if (typeof(require) == 'function') delete window.module;
</script>

<script src='/res/lib/utils.js.gzip'></script>
<script src='/res/lib/laydate/laydate.js.gzip'></script>
<script src='/res/lib/highlight/highlight.js.gzip'></script>
<script src='/res/lib/kindeditor/kindeditor.js.gzip'></script>
<script src='/res/lib/kindeditor/lang/zh-cn.js.gzip'></script>
<script src='/res/lib/codemirror/lib/codemirror.js.gzip'></script>

<script src='/res/lib/codemirror/mode/clike/clike.js'></script>
<script src='/res/lib/codemirror/mode/shell/shell.js'></script>
<script src='/res/lib/codemirror/mode/markdown/markdown.js'></script>

<script src='/res/lib/codemirror/addon/fold/foldcode.js'></script>
<script src='/res/lib/codemirror/addon/fold/foldgutter.js'></script>
<script src='/res/lib/codemirror/addon/fold/brace-fold.js'></script>
<script src='/res/lib/codemirror/addon/fold/indent-fold.js'></script>
<script src='/res/lib/codemirror/addon/fold/comment-fold.js'></script>

<style>
#TopDiv{
	background: rgba(10, 80, 10, 0.8);
}
#SideDiv{
	overflow: hidden;
	border-right: 1px solid #ABC;
	border-top: 1px solid rgba(10, 80, 10, 0.8);
}
#MainDiv{
	border-top: 1px solid rgba(10, 80, 10, 0.8);
}
#MenuDiv div{
	font-weight: bold;
}
#TitleDiv{
	top: 0px;
	right: 0px;
	height: 100%;
	position: absolute;
	display: inline-block;
}
.TitleLink{
	color: #DDD;
	padding: 2px 4px;
}
.TitleLink:hover{
	color: #0AA;
}
#BottomDiv{
	background-color: rgba(10, 80, 10, 0.8);
}
#ToggleButton{
	width: 30px;
	height: 30px;
	margin-top: 1px;
	margin-left: 1px;
	background-size: 20px 20px;
	background-position: center;
	background-repeat: no-repeat;
	background-image: url(/res/img/list.png);
}
</style>

<script>
let menugroup = [];
let clickflag = null; 
let menutitle = null;
let menufolder = null;
var saveneeded = null;
var submenuicon = null;
var submenulist = null;
var curmenuitem = null;
var togglebutton = null;
var minimizeside = false;
var automodifymap = {};
var usergrouplist = null;
var modifyNotepad = null;

var linkmap = new Map();
var menucachemap = new Map();
let sidemenusize = {cx: 160, cy: 18};

function getLink(){
	var pos = window.location.href.indexOf('#');
	if (pos < 0) return null;
	var link = window.location.href.substring(pos + 1);
	return decodeURIComponent(link);
}
function updateTitle(){
	$('#TitleDiv').html(getHttpResult('/titlepage'));
}
function getSideWidth(){
	return $('#MainDiv').position().left;
}
function updateSubmenu(){
	for (let i = 0; i < menugroup.length; i++){
		if (menufolder == menugroup[i].text){
			removeSubmenuCache(menufolder);
			menugroup[i].click();
		}
	}
}
function hasSubmenu(title){
	if (submenulist == null) return false;
	for (let i = 0; i < submenulist.length; i++){
		if (title == submenulist[i]) return true;
	}
	return false;
}
function getSubmenu(folder){
	let key = getCurrentUser() + ':' + folder;
	let list = menucachemap.get(key);
	if (list) return list;
	getHttpResult('/getusermenu', {folder: folder}, function(data){
		if (data.code == 0){
			sessionTimeout();
		}
		else if (data.code < 0){
			showToast('加载数据失败');
		}
		else{
			menucachemap.set(key, list = data.list);
		}
	});
	return list;
}
function checkLoginStatus(){
	var code = '<%=code%>';
	var mail = '<%=session%>';

	if (strlen(mail) > 0 && strlen(code) > 0){
		getHttpResult('/checklogin', {mail: mail, code: code}, function(data){
			history.pushState('', '', '/');
			usergrouplist = data.grouplist;
			setCurrentUser(data.user);
		});
	}
	else{
		getHttpResult('/checklogin', {flag: 'C'}, function(data){
			if (data.code > 0){
				usergrouplist = data.grouplist;
				setCurrentUser(data.user);
			}
		});
	}
}
function updateMenu(updated){
	let menubar = null;
	let sideview = $('#SideDiv');
	let menuview = $('#MenuDiv');
	let contentview = $('#ContentDiv');

	saveneeded = null;
	linkmap.clear();

	checkLoginStatus();
	updateTitle();

	if (updated){
		if (minimizeside){
			togglebutton.click();
		}
		curmenuitem = null;
	}

	let menucolor = {
		color: '#088',
		hovercolor: '#000',
		selectcolor: '#088',
		barcolor: 'rgba(0, 0, 0, 0)',
		listcolor: 'rgba(0, 0, 0, 0)'
	}

	let contextmap = new Map();
	let movefolder = function(url, elem){
		let arr = [];
		let children = elem.menubar.children();
		for (let i = 0; i < children.length; i++){
			arr.push(children[i].text);
		}
		getHttpResult(url, {flag: 'M', folder: arr.join(',')}, null);
		removeSubmenuCache(elem.menubar.text);
	}
	let removefolder = function(url, elem){
		let folder = elem.text;
		showConfirmMessage('是否决定删除目录[' + folder + ']？', '删除目录', function(flag){
			if (flag){
				getHttpResult(url, {flag: 'R', folder: folder}, function(data){
					if (data.code < 0){
						showToast('删除目录失败');
					}
					else{
						removeSubmenuCache(elem.menubar.text);
						if (elem == curmenuitem){
							curmenuitem = null;
							elem.menubar.click();
						}
						else{
							if (submenulist){
								let arr = [];
								for (let i = 0; i < submenulist.length; i++){
									if (elem.text == submenulist[i]) continue;
									arr.push(submenulist[i]);
								}
								submenulist = arr;
							}
							elem.remove();
						}
					}
				});
			}
		});
	}

	contextmap.set('前移@菜单管理', function(elem){
		movefolder('/EditMenu', elem);
	});
	contextmap.set('后移@菜单管理', function(elem){
		movefolder('/EditMenu', elem);
	});
	contextmap.set('删除@菜单管理', function(elem){
		removefolder('/EditMenu', elem);
	});

	contextmap.set('前移@我的笔记', function(elem){
		movefolder('/EditNote', elem);
	});
	contextmap.set('后移@我的笔记', function(elem){
		movefolder('/EditNote', elem);
	});
	contextmap.set('删除@我的笔记', function(elem){
		removefolder('/EditNote', elem);
	});

	contextmap.set('前移@产品管理', function(elem){
		movefolder('/EditNote', elem);
	});
	contextmap.set('后移@产品管理', function(elem){
		movefolder('/EditNote', elem);
	});
	contextmap.set('删除@产品管理', function(elem){
		removefolder('/EditNote', elem);
	});

	contextmap.set('前移@我的代码', function(elem){
		movefolder('/compile/editnote', elem);
	});
	contextmap.set('后移@我的代码', function(elem){
		movefolder('/compile/editnote', elem);
	});
	contextmap.set('删除@我的代码', function(elem){
		removefolder('/compile/editnote', elem);
	});

	contextmap.set('前移@配置文件', function(elem){
		movefolder('/confile/editnote', elem);
	});
	contextmap.set('后移@配置文件', function(elem){
		movefolder('/confile/editnote', elem);
	});
	contextmap.set('删除@配置文件', function(elem){
		removefolder('/confile/editnote', elem);
	});

	let contextmenu = [{
		title: '前移',
		click: function(elem){
			let item = getContextMenuAttach();
			let func = contextmap.get('前移@' + item.menubar.text);
			item.prev().insertAfter(item);
			if (func) func(item);
		}
	}, {
		title: '后移',
		click: function(elem){
			let item = getContextMenuAttach();
			let func = contextmap.get('后移@' + item.menubar.text);
			item.next().insertBefore(item);
			if (func) func(item);
		}
	}, {
		title: '删除',
		click: function(elem){
			let item = getContextMenuAttach();
			let func = contextmap.get('删除@' + item.menubar.text);
			if (func) func(item);
		}
	}];

	menuview.children().remove();
	menuview.width(sideview.width());
	menuview.height(sideview.height());
	sideview.css('background', menucolor.listcolor);

	function updateView(url){
		clearSingletonInterval();
		if (saveneeded){
			saveneeded(function(){
				contentview.html(getHttpResult(url));
				saveneeded = null;
			});
		}
		else{
			contentview.html(getHttpResult(url));
		}
	}

	let homemenubar = new MenuBar(menuview, '关于我们', '/res/img/menu/home.png', menugroup = [], sidemenusize.cy);
	linkmap.set('关于我们', homemenubar);
	homemenubar.bar.click(function(){
		clickflag = true;
		setCurrentMenu(homemenubar);
		window.location.href = '#关于我们';
		updateView('/app/workspace/pub/about.htm');
		for (let i = 0; i < menugroup.length; i++){
			if (homemenubar == menugroup[i]){
				menugroup[i].select();
			}
			else{
				menugroup[i].unselect();
			}
		}
	});
	homemenubar.color(menucolor);
	homemenubar.tag.hide();

	getHttpResult('/getusermenu', null, function(data){
		if (data.code < 0){
			showToast('加载数据失败');
		}
		else{
			for (let i = 0; i < data.list.length; i++){
				let menu = null;
				let folder = data.list[i].folder;
				if (folder == "我的笔记" || folder == "我的代码" || folder == "产品管理" || folder == "配置文件" || folder == '菜单管理'){
					menu = new MenuBar(menuview, folder, data.list[i].icon, menugroup, sidemenusize.cy, null, contextmenu);
				}
				else{
					menu = new MenuBar(menuview, folder, data.list[i].icon, menugroup, sidemenusize.cy, null, null);
				}
				function loadSubmenu(){
					let first = null;
					let submenu = null;
					let menulist = getSubmenu(folder);
					if (menulist){
						submenulist = [];
						menu.clear();
						for (let j = 0; j < menulist.length; j++){
							let url = menulist[j].url;
							let icon = menulist[j].icon;
							let title = menulist[j].title;
							let mitem = menu.add(title, icon, function(){
								updateView(url);
								clickflag = true;
								submenuicon = icon;
								setCurrentMenu(mitem);
								window.location.href = '#' + folder + '@' + title;
							});
							let name = getFileName(url).toLowerCase();
							if (name == 'addfolder.htm' || name == 'addnotefolder.htm' || name == 'addmenufolder.htm'){
								getCtrl(mitem).onmouseup = null;
							}
							if (isCurrentMenu(mitem)) submenu = mitem;
							linkmap.set(folder + '@' + title, mitem);
							if (first == null) first = mitem;
							submenulist.push(title);
						}
						if (first && submenu == null && folder == menufolder){
							submenu = first;
						}
						if (submenu){
							submenu.select(curmenuitem ? false : true);
							curmenuitem = submenu;
							menu.open();
						}
					}
				}
				if (menubar == null || folder == menufolder) menubar = menu;
				menu.click(loadSubmenu);
				menu.color(menucolor);
			}
			if (menubar) menubar.click();
		}
	});

	if (curmenuitem == null){
		curmenuitem = homemenubar;
		homemenubar.bar.click();
	}
}
function setSaveNeeded(needed){
	saveneeded = needed;
}
function autoModifyContentSize(id){
	automodifymap[id] = id;
}
function removeSubmenuCache(folder){
	menucachemap.delete(getCurrentUser() + ':' + folder);
}
function isCurrentMenu(submenu){
	return menufolder == submenu.menubar.text && menutitle == submenu.text;
}
function setCurrentMenu(submenu){
	if (submenu.menubar){
		menufolder = submenu.menubar.text;
		menutitle = submenu.text;
	}
	else{
		menufolder = submenu.text;
		menutitle = null;
	}
	curmenuitem = submenu;
}
function selectMenu(submenu, updated){
	if (submenu == null){
		updateMenu();
	}
	else{
		setCurrentMenu(submenu);
		curmenuitem = null;
		if (submenu.menubar){
			for (let i = 0; i < menugroup.length; i++){
				if (menufolder == menugroup[i].text){
					removeSubmenuCache(menufolder);
					menugroup[i].click();
				}
			}
		}
		else{
			submenu.click();
		}
	}
}

window.onload = function(){
	minimizeside = true;
	togglebutton = $('#ToggleButton');

	setFrameStyle('TopDiv', 'SideDiv', 'MainDiv', 'BottomDiv', 30, sidemenusize.cx, 34, 0);
	setLoginCallback(function(){
		hideMsgBox();
		hideToastBox();
		updateMenu(true);
	});

	setResizeCallback('menudiv', function(){
		for (let i = 0; i < menugroup.length; i++){
			menugroup[i].hideToast();
		}
		$('#MenuDiv').height($('#SideDiv').height());
	});

	setMouseupCallback('menubar', function(){
		for (let i = 0; i < menugroup.length; i++){
			menugroup[i].hideToast();
		}
	});

	$(window).resize(function(){
		let width = getCtrl('MainDiv').scrollWidth;
		for (let id in automodifymap) $.pack(id).width(width);
		if (modifyNotepad) modifyNotepad();
	});

	let lastlink = getLink();
	if (lastlink){
		let arr = lastlink.split('@');
		if (arr.length > 1) menutitle = arr[1];
		if (arr.length > 0) menufolder = arr[0];
	}

	function popstate(){
		let link = getLink();
		if (link && link != lastlink){
			let item = linkmap.get(link) || curmenuitem;
			if (item){
				if (clickflag){
					clickflag = false;
				}
				else{
					if (item.menubar){
						item.menubar.open();
						item.click();
					}
					else{
						item.bar.click();
					}
				}
			}
			lastlink = link;
		}
	}

	window.onhashchange = popstate;
	window.onpopstate = popstate;
	updateMenu();

	let querydiv = $('#QueryBarDiv');
	let bottomview = $('#BottomDiv');

	function toggle(){
		if (minimizeside){
			for (let i = 0; i < menugroup.length; i++){
				menugroup[i].maximize();
			}
			setFrameStyle('TopDiv', 'SideDiv', 'MainDiv', 'BottomDiv', 30, sidemenusize.cx, 34, 0);
			togglebutton.css('transform', 'rotate(0deg)').attr('title', '点击收起导航栏');
			querydiv.css('left', '160px');
			minimizeside = false;
		}
		else{
			for (let i = 0; i < menugroup.length; i++){
				menugroup[i].minimize();
			}
			setFrameStyle('TopDiv', 'SideDiv', 'MainDiv', 'BottomDiv', 30, sidemenusize.cy + 14, 34, 0);
			togglebutton.css('transform', 'rotate(90deg)').attr('title', '点击展开导航栏');
			querydiv.css('left', '32px');
			minimizeside = true;
		}

		$(window).resize();
	}

	bottomview.load('/sharenote?flag=S&title=FOOTER');
	togglebutton.click(toggle);
	toggle();
}
</script>
</head>

<body>
	<div id='TopDiv'>
		<div id='ToggleButton'></div>
		<div id='StatusLabel'></div>
		<div id='TitleDiv'></div>
	</div>
	<div id='SideDiv'>
		<div style='width:100%;height:100%;position:relative;overflow:hidden'>
			<div style='overflow-x:hidden;overflow-y:scroll;position:absolute'>
				<div id='MenuDiv'></div>
			</div>
		 </div>
	</div>
	<div id='MainDiv' class='HiddenScrollbar'>
		<div id='ContentDiv'></div>
	</div>
	<div id='BottomDiv'></div>
</body>
</html>